import {
  EggBaseError,
  EggBaseException,
  ErrorOptions,
} from 'egg-errors';

class BizBaseErrorOptions extends ErrorOptions {
  public data?: object;
}

class BizBaseError extends EggBaseError<BizBaseErrorOptions> {
  public data?: object;
  protected options: BizBaseErrorOptions;

  constructor (options?: BizBaseErrorOptions) {
    super(options);

    this.data = this.options.data;
  }
}

class BizErrorOptions {
  public code?: string;
  public message?: string;
  public data?: object;
  [key: string]: any;
}

class BizError extends BizBaseError {
  constructor(message: string = '', options: BizErrorOptions = {}) {
    super({
      originnMessage: options.message || '',
      ...options,
      message,
    });
  }

  public static check(err: Error): boolean {
    return err.name === 'BizError';
  }
}

class BizExceptionOptions extends ErrorOptions {
  public data?: object;
}

class BizException extends EggBaseException<BizExceptionOptions> {
  public data?: object;
  protected options: BizExceptionOptions;

  constructor (options?: BizExceptionOptions) {
    super(options);

    this.data = this.options.data;
  }

  public static check(err: Error): boolean {
    return err.name === 'BizException';
  }
}

export {
  BizError,
  BizException,
};
